package io;

import java.io.*;

public class FileOperate {
    public void copyFile(File file, String path){
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(file); //被复制的输入流
            File newFile = new File(path + file.getName()); //新的文件对象
            outputStream = new FileOutputStream(newFile); //新的文件的输出流
            byte[] b = new byte[1024]; //字节数组
            int count = inputStream.read(b);//count表示输入流的字节数
            while (count != -1){
                outputStream.write(b,0,count);
                outputStream.flush();
                count = inputStream.read(b);
            }

        } catch (java.io.IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void encryptionFile(File file, String path){
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(file); //被复制的输入流
            File newFile = new File(path + file.getName()); //新的文件对象
            outputStream = new FileOutputStream(newFile); //新的文件的输出流
            byte[] b = new byte[1024]; //字节数组
            int count = inputStream.read(b);//count表示输入流的字节数
            while (count != -1){
                byte temp = b[0];
                b[1] = b[0];
                b[0] = b[1];
                outputStream.write(b,0,count);
                outputStream.flush();
                count = inputStream.read(b);
            }
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //文件夹的复制
    public void superCopyFile(File file, String path){
        //获取文件夹的绝对路径 拼串的方式获取新文件的名字
        String oldFilePath = file.getAbsolutePath();
        String newFilePath = path + oldFilePath.split(":")[1];
        //创建一个新的file对象
        File newFile = new File(newFilePath);

        //判断当前传过来的file是文件夹还是文件
        File[] files = file.listFiles();
        if (files != null){ //file是一个文件夹，才有数组对象
            //创建目录
            newFile.mkdir();
            //
            if(files.length != 0){
                for(File f : files){
                    superCopyFile(f, path);
                }
            }

        } else {
            FileInputStream inputStream = null;
            FileOutputStream outputStream = null;
            try {
                inputStream = new FileInputStream(file); //被复制的输入流
                outputStream = new FileOutputStream(newFile); //新的文件的输出流
                byte[] b = new byte[1024]; //字节数组
                int count = inputStream.read(b);//count表示输入流的字节数
                while (count != -1){
                    outputStream.write(b,0,count);
                    outputStream.flush();
                    count = inputStream.read(b);
                }

            } catch (java.io.IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null){
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }

    public static void main(String[] args) {
        File file = new File("C:\\qwer");
        FileOperate operate = new FileOperate();
        operate.superCopyFile(file,"D:\\test");
    }
}
